import Line from '../elements/line';
import Text from '../elements/text';
import * as _ from '../utils';
const yAxisAction = function (yAxis, origin) {
    let show = _.getObjProperty(yAxis, 'show', true);
    let splitLine = _.getObjProperty(yAxis, 'splitLine', {});
    let splitLineShow = _.getObjProperty(splitLine, 'show', true);
    let elements = [];
    let yOrigin = {};
    let data = yAxis.data;
    let yAxisLen = data.length;
    let xAxisLen = yAxis.xAxisLen;
    let yMax = data[yAxisLen - 1];

    let x = origin.x;
    let y = origin.y;
    let w = origin.w;
    let h = origin.h;

    let yAxisWidth = _.numFormat(yMax).length * 8 + 5;
    let lineLen = parseInt((w - yAxisWidth) / xAxisLen) * xAxisLen;

    yOrigin.yAxisWidth = yAxisWidth;
    for (let i = 0; i < yAxisLen; i++) {
        let p_x = x + yAxisWidth - 5;
        let p_y = parseInt((h * i) / (yAxisLen - 1)) + y;
        let val = _.numFormat(data[yAxisLen - 1 - i]);
        let textStyle = {
            text: val,
            fillStyle: '#333333',
            textAlign: 'right',
            fontSize: 12,
        };
        let textPoints = [
            {
                x: p_x,
                y: p_y + 1,
            },
        ];

        let yText = new Text({
            points: textPoints,
            style: textStyle,
        });
        show && elements.push(yText);

        // if (i < yAxisLen - 1) {
        let points = [
            {
                x: x + yAxisWidth,
                y: p_y,
            },
            {
                x: x + yAxisWidth + lineLen,
                y: p_y,
            },
        ];
        let style = {
            strokeStyle: '#dddddd',
            lineWidth: 1,
            lineDash: [4, 3],
            lineDashOffset: 0,
        };
        let supLine = new Line({
            points,
            style,
        });
        splitLineShow && elements.push(supLine);
        // }
    }

    return {
        elements,
        yOrigin,
    };
};
export default yAxisAction;
